package com.tools.controller;

import cn.hutool.core.io.IoUtil;

import com.tools.common.beans.constant.ControllerConstant;
import com.tools.common.beans.vo.Result;
import com.tools.generator.entity.FieldTypeEntity;
import com.tools.generator.entity.ProjectModifyEntity;
import com.tools.generator.service.ProjectModifyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 项目名变更
 *
 * @author 阿沐 babamu@126.com
 * <a href="https://maku.net">MAKU</a>
 */
@RestController
@RequestMapping(ControllerConstant.API_PROJECT_PREFIX)
public class ProjectModifyController extends BaseController{

    @Autowired
    private  ProjectModifyService projectModifyService;

    private final String prefix = "generate/";

    @GetMapping(value = {"/{page}.html"})
    public ModelAndView allPage(@PathVariable("page") String page){
        return jumpPage(prefix + page);
    }

    @GetMapping(value = {"/project-edit"})
    public ModelAndView editPage(@RequestParam("id") Long id){
        ProjectModifyEntity byId = projectModifyService.getById(id);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("project",byId);
        modelAndView.setViewName(prefix + "project-edit");
        return modelAndView;
    }

    @PostMapping("page")
    public Result page(@Valid ProjectModifyEntity query) {
        startPage(query.getPage(), query.getLimit());
        List<ProjectModifyEntity> page = projectModifyService.page(query);
        return Result.OK(page);
    }

    @GetMapping("{id}")
    public Result<ProjectModifyEntity> get(@PathVariable("id") Long id) {
        ProjectModifyEntity entity = projectModifyService.getById(id);
        return Result.OK(entity);
    }

    @PostMapping("save")
    public Result<String> save(@RequestBody ProjectModifyEntity entity) {
        projectModifyService.save(entity);
        return Result.OK();
    }

    @PutMapping("update")
    public Result<String> update(@RequestBody ProjectModifyEntity entity) {
        projectModifyService.updateById(entity);
        return Result.OK();
    }

    @DeleteMapping("delete")
    public Result<String> delete(@RequestBody List<Long> idList) {
        projectModifyService.removeByIds(idList);
        return Result.OK();
    }

    /**
     * 源码下载
     */
    @GetMapping("download/{id}")
    public void download(@PathVariable("id") Long id, HttpServletResponse response) throws Exception {
        // 项目信息
        ProjectModifyEntity project = projectModifyService.getById(id);
        byte[] data = projectModifyService.download(project);
        response.reset();
        response.setHeader("Content-Disposition", "attachment; filename=\"" + project.getModifyProjectName() + ".zip\"");
        response.addHeader("Content-Length", "" + data.length);
        response.setContentType("application/octet-stream; charset=UTF-8");
        IoUtil.write(response.getOutputStream(), false, data);
    }
}